from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Text
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from database import Base


class OperationLog(Base):
    """
    操作日志模型
    """
    __tablename__ = "operation_logs"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=True)
    username = Column(String(50), nullable=True)
    operation_time = Column(DateTime(timezone=True), server_default=func.now())
    ip_address = Column(String(50), nullable=True)
    operation_type = Column(String(50), nullable=False)  # 操作类型：login, create_task, update_route等
    operation_content = Column(Text, nullable=False)  # 操作内容描述
    success = Column(Integer, default=1)  # 操作是否成功：1成功，0失败
    error_message = Column(Text, nullable=True)  # 错误信息

    # 关联关系
    user = relationship("User", backref="operation_logs")